﻿namespace MatrixField
{
    using System;
    using System.Linq;
    using System.Text;

    public class Matrix
    {
        private readonly int[,] matrix;
        private int row;
        private int col;
        private int valueCell;

        public Matrix(int size)
        {
            this.matrix = new int[size, size];
            this.ValueCell = 1;
        }

        public int Row 
        {
            get
            {
                return this.row;
            }

            set
            {
                if (value < 0 || value >= this.Size)
                {
                    throw new ArgumentOutOfRangeException(string.Format("Row should be between 0 and {0}", this.Size));
                }

                this.row = value;
            }
        }

        public int Col
        {
            get
            {
                return this.col;
            }

            set
            {
                if (value < 0 || value >= this.Size)
                {
                    throw new ArgumentOutOfRangeException(string.Format("Col should be between 0 and {0}", this.Size));
                }

                this.col = value;
            }
        }

        public int Size
        {
            get
            {
                return this.matrix.GetLength(0);
            }
        }

        public int ValueCell
        {
            get
            {
                return this.valueCell;
            }

            //// imam problem pri matrica 4*4
            set
            {
                if (value < 1 || value > this.Size * this.Size)
                {
                    throw new ArgumentOutOfRangeException(string.Format("Value of the cells is between 1 and {0}", this.Size * this.Size));
                }

                this.valueCell = value;
            }
        }

        public int this[int row, int col]
        {
            get
            {
                return this.matrix[row, col];
            }

            set
            {
                if (row < 0 || col < 0 || row >= this.Size || col >= this.Size)
                {
                    throw new ArgumentException("Index CANNOT be negative or bigger than the size of the matrix!");
                }

                this.matrix[row, col] = value;
            }
        }

        public override string ToString()
        {
            StringBuilder build = new StringBuilder();

            for (int i = 0; i < this.Size; i++)
            {
                for (int j = 0; j < this.Size; j++)
                {
                    if (j==this.Size - 1)
                    {
                        build.AppendFormat("{0}", this.matrix[i, j]);
                    }
                    else
                    {
                        build.AppendFormat("{0,-3}", this.matrix[i, j]);
                    }
                }

                build.Append("\n");
            }

            return build.ToString().Trim();
        }
    }
}